#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstring>
using namespace std;
int N;
constexpr int MAXN = 12;
vector<int> Nums;
void solve();

int main() {
    ios::sync_with_stdio(false);
    cin >> N;
    cin.get();
    string line;
    while (N--) {
        Nums.clear();
        getline(cin, line);
        stringstream ss(line);
        int x;
        while (ss >> x) {
            Nums.push_back(x);
        }
        solve();
    }
}

bool used[MAXN];
int mid = 1;
vector<int> target;
int mini = 1 << 30;
int calculate() {
    if (target[0] == 0 && mid > 1) return mini;
    if (target[mid] == 0 && target.size() - mid > 1) return mini;
    int sum_prex = 0, i = 0, sum_end = 0;
    while (i < mid) {
        sum_prex = sum_prex * 10 + target[i];
        i++;
    }
    while (i < target.size()) {
        sum_end = sum_end * 10 + target[i];
        i++;
    }
    return abs(sum_prex - sum_end);
}

void permulate() {
    if (target.size() == Nums.size()) {
        mini = min(mini, calculate());
        return;
    }
    for (int i = 0; i < Nums.size(); i++) {
        if (!used[i]) {
            target.push_back(Nums[i]);
            used[i] = true;
            permulate();
            target.pop_back();
            used[i] = false;
        }
    }
}
// 本质上是一个全排列
void solve() {
    memset(used, 0, sizeof used);
    mid = Nums.size() / 2;
    mini = 1 << 30;
    permulate();
    cout << mini << endl;
    return;
}